package leetcode;

import util.Tree;
import util.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0102.java
 * @Author : WangRuoyu
 * @Date 2023/10/22 16:30
 */

public class Q0102 {

    List<List<Integer>> res = new ArrayList<>();

    private void dfs(TreeNode node, int k) {
        if (node == null) return;
        if (k > res.size()) res.add(new ArrayList<>());
        res.get(k - 1).add(node.val);
        dfs(node.left, k + 1);
        dfs(node.right, k + 1);
    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        dfs(root, 1);
        return res;
    }

    public static void main(String[] args) {
        Q0102 sl = new Q0102();
        String[] tree = {"3", "9", null, null, "20", "15", null, null, "7", null, null};
        TreeNode root = Tree.createTreeByPreorder(tree);
        System.out.println(sl.levelOrder(root));
    }
}